package org.common.utils.rpc.core;
import com.esotericsoftware.reflectasm.MethodAccess;
import java.lang.reflect.Method;

/**
 * RPC方法实现
 * @author zhouzhibing
 * @date 2025/11/16 20:51
 */
public class RpcMethodImpl {

    /**
     * 实例
     */
    private final Object instance;

    /**
     * 方法通道
     */
    private final MethodAccess methodAccess;

    /**
     * 方法对象
     */
    private final Method method;

    /**
     * 方法索引
     */
    private final int methodIndex;


    public RpcMethodImpl(Object instance , MethodAccess methodAccess , Method method) {
        this.instance = instance;
        this.methodAccess = methodAccess;
        this.method = method;
        this.methodIndex = methodAccess.getIndex(method.getName(), method.getParameterTypes());
    }

    /**
     * 调用方法
     * @param args 参数
     * @return 方法返回值
     */
    public Object invoke(Object... args) {
        return methodAccess.invoke(instance, methodIndex, args);
    }

}
